// pages/personal/personal.js
import request from '../../utils/request'
let startY = 0
let moveY = 0
let moveDistance = 0

Page({
  data: {
    coverTransform: 'translateY(0)',
    coverTransition: '',
    userInfo: {},
    recentPlayList: []
  },

  onLoad(options) {
    this.getUserInfo()
    this.getUserRecentPlay()
  },
  getUserInfo() {
    let userInfo = wx.getStorageSync('userInfo') ? JSON.parse(wx.getStorageSync('userInfo')) : {}
    if (userInfo) {
      this.setData({
        userInfo
      })
    }
  },
  async getUserRecentPlay() {
    const {
      userInfo
    } = this.data
    try {
      let res = await request("user/record", {
        type: 0,
        uid: userInfo.userId
      })
      this.setData({
        recentPlayList: res.allData.slice(0, 10)
      })
    } catch (error) {
      console.log(error);
    }

  },
  handleTouchStart(e) {
    this.setData({
      coverTransform: `translateY(0)`,
      coverTransition: ''
    })
    startY = e.touches[0].clientY
  },
  handleTouchMove(e) {
    moveY = e.touches[0].clientY
    moveDistance = moveY - startY
    if (moveDistance <= 0) {
      return
    }
    if (moveDistance >= 160) {
      moveDistance = 160
    }
    this.setData({
      coverTransform: `translateY(${moveDistance}rpx)`
    })
  },
  handleTouchEnd(e) {
    this.setData({
      coverTransform: `translateY(0)`,
      coverTransition: 'transform 0.4s linear'
    })
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  onShareAppMessage() {},
  onShareTimeline() {}

})